/*
 *
 *  * Licensed to the Apache Software Foundation (ASF) under one or more
 *  * contributor license agreements.  See the NOTICE file distributed with
 *  * this work for additional information regarding copyright ownership.
 *  * The ASF licenses this file to You under the Apache License, Version 2.0
 *  * (the "License"); you may not use this file except in compliance with
 *  * the License.  You may obtain a copy of the License at
 *  *
 *  *     http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

package com.slydm.thinking.in.spring.ioc.overview.container;

import com.slydm.thinking.in.spring.ioc.overview.domain.User;
import java.util.Map;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

/**
 * IOC容器示例
 *
 * @author wangcymy@gmail.com(wangcong) 2020/10/23 下午9:05
 */
public class IocContainerDemo {

  public static void main(String[] args) {

    // 创建BeanFactory容器
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);

    // xml配置文件路径
    String location = "META-INF/dependency-lookup-context.xml";
    int beanDefinitionsCount = reader.loadBeanDefinitions(location);

    System.out.println(beanDefinitionsCount);

    lookupCollectionByType(beanFactory);

  }

  private static void lookupCollectionByType(BeanFactory beanFactory) {

    if (beanFactory instanceof ListableBeanFactory) {
      ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;

      Map<String, User> users = listableBeanFactory.getBeansOfType(User.class);
      System.out.println("查找到的所有User对象: " + users);

    }

  }

}
